package com.xuan.mail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.mail.domin.Contact;
import com.xuan.mail.domin.ContactGroup;
import com.xuan.mail.domin.Groupm;
import com.xuan.mail.mapper.ContactGroupMapper;
import com.xuan.mail.mapper.GroupmMapper;
import com.xuan.mail.service.ContactService;
import com.xuan.mail.mapper.ContactMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86156
* @description 针对表【contact】的数据库操作Service实现
* @createDate 2024-03-15 14:20:38
*/
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact>
    implements ContactService{
    @Resource
    private ContactMapper contactMapper;
    @Resource
    private ContactGroupMapper contactGroupMapper;
    @Resource
    private GroupmMapper groupmMapper;

    @Override
    public List<Contact> getAllContact() {
        List<Contact> contactList = contactMapper.selectList(new LambdaQueryWrapper<Contact>()
                .eq(Contact::getIsdeleted, 0)
        );
        return contactList;


    }

    @Override
    public String getGroupNameById(Integer contactId) {
        return null;
    }

//    @Override
//    public List<Contact> getContactByGroupName(String groupName) {
//        return null;
//    }

    @Override
    public List<Contact> getContactByGroupId(Integer groupId) {
        //通过组别获取联系人
        LambdaQueryWrapper<ContactGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContactGroup::getGroupId, groupId);

        List<ContactGroup> contactGroupList = contactGroupMapper.selectList(queryWrapper);
        System.out.println(contactGroupList+"\t");
        List<Contact> contactList=new ArrayList<>();
        for (ContactGroup contactGroup : contactGroupList) {
            // 对每个contactGroup做操作
            List<Contact> contactList1 = contactMapper.selectList(new LambdaQueryWrapper<Contact>()
                    .eq(Contact::getContactId, contactGroup.getContactId())
                    .eq(Contact::getIsdeleted, 0)
            );
            for (Contact contact : contactList1){
                contactList.add(contact);
            }
        }
        System.out.println(contactList);
      return contactList;
    }

    @Override
    public void addContact(Contact contact, Integer groupId) {
        System.out.println("输入:"+contact.getContactId()+contact.getContactName()+contact.getEmail()+contact.getIsdeleted());
        Contact existingContact = contactMapper.selectOne(new LambdaQueryWrapper<Contact>()
                        .eq(Contact::getContactName, contact.getContactName())
                        .eq(Contact::getEmail, contact.getEmail())
                        .eq(Contact::getUserId, contact.getUserId())
                        .eq(Contact::getIsdeleted, 1)
                );
        System.out.println(existingContact);

        if (existingContact!=null) {
            // 如果存在，执行更新操作
            // 这里假设你要更新contact的所有字段，你可以根据需要选择性地更新某些字段
            System.out.println("update:"+contact.getContactId()+contact.getContactName()+contact.getEmail()+contact.getIsdeleted());
            contactMapper.update(contact, new LambdaUpdateWrapper<Contact>()
                    .eq(Contact::getContactId, contact.getContactId())
                    .eq(Contact::getContactName, contact.getContactName())
                    .eq(Contact::getEmail, contact.getEmail())
                    .eq(Contact::getUserId, contact.getUserId())
                    .set(Contact::getIsdeleted,0)

            );
            System.out.println("update2:"+contact.getContactId()+contact.getContactName()+contact.getEmail()+contact.getIsdeleted());
        }else {
            // 如果不存在，执行插入操作
            contact.setIsdeleted(0);
            contactMapper.insert(contact);
            System.out.println("save:"+contact.getContactId()+contact.getContactName()+contact.getEmail()+contact.getIsdeleted());
            ContactGroup contactGroup = new ContactGroup();
            contactGroup.setContactId(contact.getContactId());
            contactGroup.setGroupId(groupId);
            System.out.println("contactGroup:"+contactGroup.getContactId()+contactGroup.getGroupId());
            contactGroupMapper.insert(contactGroup);
        }
    }

    @Override
    public void logicdeleteContact(Integer contactId) {
        Contact contact = contactMapper.selectById(contactId);
        contactMapper.update(contact, new LambdaUpdateWrapper<Contact>()
                .eq(Contact::getContactId, contact.getContactId())
                .set(Contact::getIsdeleted,1)
        );

    }

    @Override
    public void deleteContact(Integer contactId) {
        contactMapper.deleteById(contactId);
        // 同时删除联系人分组表中的数据

        contactGroupMapper.delete(new LambdaQueryWrapper<ContactGroup>()
                .eq(ContactGroup::getContactId, contactId));
        System.out.println("delete:"+contactId);
    }

    @Override
    public void updateContact(Contact contact,Integer groupId) {
        contactMapper.updateById(contact);
        // 同时更新联系人分组表中的数据
        contactGroupMapper.update(new ContactGroup(), new LambdaUpdateWrapper<ContactGroup>()
                .eq(ContactGroup::getContactId, contact.getContactId())
                .set(ContactGroup::getGroupId, groupId));
    }

    @Override
    public List<Contact> getContactAll(Integer userId) {
        // 查询所有联系人
        List<Contact> contactList = contactMapper.selectList(new LambdaQueryWrapper<Contact>()
                    .eq(Contact::getUserId, userId)
                    .eq(Contact::getIsdeleted, 0)
            );
        System.out.println(contactList);
        return contactList;
    }

    @Override
    public Groupm getGroupByContactId(Integer contactId) {
        //根据联系人id获取组别id
        LambdaQueryWrapper<ContactGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContactGroup::getContactId, contactId);
        List<ContactGroup> contactGroupList = contactGroupMapper.selectList(queryWrapper);

        if (contactGroupList.isEmpty()) {
            return null;
        }

        Integer groupId = contactGroupList.get(0).getGroupId();

        // 根据组别id获取组别信息
        LambdaQueryWrapper<Groupm> groupQueryWrapper = new LambdaQueryWrapper<>();
        groupQueryWrapper.eq(Groupm::getGroupId, groupId).eq(Groupm::getIsdeleted, 0);
        List<Groupm> groupList = groupmMapper.selectList(groupQueryWrapper);

        for (Groupm group : groupList){
            return group;
        }
        return null;



    }

    @Override
    public Contact getContactById(Integer contactId) {
        LambdaQueryWrapper <Contact> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Contact::getContactId, contactId);
        queryWrapper.eq(Contact::getIsdeleted, 0);
        Contact contact = contactMapper.selectOne(queryWrapper);
        return contact;
    }


}




